package com.lkd.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.dto.PolicyDTO;
import com.lkd.entity.Policy;
import com.lkd.entity.VM;
import com.lkd.mapper.PolicyMapper;
import com.lkd.mapper.VmMapper;
import com.lkd.result.TableInfo;
import com.lkd.service.PolicyService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: Z.lx
 * @Date: 2025/8/23 14:30
 * @Description:
 **/
@Service
@AllArgsConstructor
public class PolicyServiceImpl extends ServiceImpl<PolicyMapper, Policy> implements PolicyService {
    private final VmMapper vmMapper;
    private final PolicyMapper policyMapper;

    /**
    * @Description 获取策略
    * @Param [innerCode]
    * @Date 2025/8/23 14:36
    **/
    public Policy getPolicyByInnerCode(String innerCode) {
        VM vm = vmMapper.selectOne(Wrappers.<VM>lambdaQuery().eq(VM::getInnerCode, innerCode));
        return policyMapper.selectOne(Wrappers.<Policy>lambdaQuery().eq(Policy::getPolicyId, vm.getPolicyId()));
    }

    /**
    * @Description 删除策略
    * @Param [id]
    * @Date 2025/8/23 14:44
    **/
    public boolean deletePolicy(String id) {
        return policyMapper.deleteById(id) > 0;
    }

    /**
    * @Description 取消策略
    * @Param [policyId, innerCode]
    * @Date 2025/8/23 14:45
    **/
    public boolean cancelPolicy(String policyId, String innerCode) {
        VM vm = vmMapper.selectOne(Wrappers.<VM>lambdaQuery()
                .eq(VM::getInnerCode, innerCode));
        vm.setPolicyId(null);
        return vmMapper.updateById(vm) > 0;
    }

    @Override
    public Boolean add(Policy policy) {
        policy.setCreateTime(LocalDateTime.now());
        return  policyMapper.insert(policy) > 0;
    }

    /**
     * @Description 根据策略搜索售货机
     * @Param []
     * @Date 2025/8/23 14:53
     **/
    public TableInfo<VM> vmList(Long policyId, PolicyDTO policyDTO) {
        PageHelper.startPage(policyDTO.getPageIndex(), policyDTO.getPageSize());
        List<VM> vmList = vmMapper.selectList( Wrappers.<VM>lambdaQuery().eq(VM::getPolicyId, policyId));
        Page<VM> page = (Page<VM>)vmList;
        return new TableInfo<>(policyDTO.getPageIndex(), policyDTO.getPageSize(), page.getPages(), page.getTotal(), vmList);
    }

    @Override
    public TableInfo<Policy> searchPolicy(PolicyDTO policyDTO) {
        PageHelper.startPage(policyDTO.getPageIndex(), policyDTO.getPageSize());
        List<Policy> policy = policyMapper.selectList(Wrappers.<Policy>lambdaQuery()
                .like(policyDTO.getPolicyName() != null,Policy::getPolicyName, policyDTO.getPolicyName()));
        Page< Policy> page = (Page< Policy>)policy;
        return new TableInfo<>(policyDTO.getPageIndex(), policyDTO.getPageSize(), page.getPages(), page.getTotal(), policy);
    }

}
